//
//  ParentPhotoListViewController.m
//  FaceRecognition
//
//  Created by syun on 2018/1/23.
//  Copyright © 2018年 Sylar. All rights reserved.
//

#import "ParentPhotoListViewController.h"
#import "MyCollectionViewCell.h"
#import "ManagerPhotoModel.h"
@interface ParentPhotoListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *myCollectionView;
@property (nonatomic,strong) NSMutableArray * dataArray;
@property (nonatomic,strong) UILabel * nohisLabel;
@end

@implementation ParentPhotoListViewController

static NSString *const cellId = @"cellId";

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        
        _dataArray = [[NSMutableArray alloc]init];
    }
    
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    
    self.title = @"底片管理";
    self.navigationItem.leftBarButtonItem = left;
    
    
    //请求家长列表
    [self requestChileParent];
    
    [self mainUI];
    // Do any additional setup after loading the view.
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)mainUI{
    //添加myCollectionView
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight - SafeAreaBottomHeight) collectionViewLayout:layout];
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [_myCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:cellId];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    _myCollectionView.backgroundColor = [UIColor whiteColor];
    _myCollectionView.collectionViewLayout = layout;
    _myCollectionView.pagingEnabled = YES;
    _myCollectionView.showsVerticalScrollIndicator = NO;
    _myCollectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_myCollectionView];
    
    self.nohisLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, SCREENWIDTH, 15)];
    [self.nohisLabel setFont:[UIFont systemFontOfSize:14]];
    [self.nohisLabel setText:@"暂无信息"];
    [self.nohisLabel setTextColor:[UIColor colorWithHexString:@"#979797"]];
    self.nohisLabel.textAlignment = NSTextAlignmentCenter;
    self.nohisLabel.hidden = YES;
    
    [_myCollectionView addSubview:self.nohisLabel];
}

#pragma mark--UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
    
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    float h = SCREENWIDTH/2 - 60;
    
    return CGSizeMake(SCREENWIDTH/2, h + 70);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ManagerPhotoModel * model =[self.dataArray objectAtIndex:indexPath.row];
    
    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.selectBtn.hidden = YES;
    [cell setDataForCell:model];
    
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}


//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//数据请求
- (void)requestChileParent
{
    RequestParam *param = [RequestParam param];
    param.studentid = self.model.studentid;
    param.scid = self.model.scid;
    NSString * method = @"api/getChildParent";
    
    [Utils Java_get:method parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"arr = %@",arr);
        
        if (arr.count > 0) {
            
            _nohisLabel.hidden = YES;
            
             self.dataArray = [ManagerPhotoModel mj_objectArrayWithKeyValuesArray:arr];
        }else
        {
            _nohisLabel.hidden = NO;
        }
       
        
        NSLog(@"self.dataArray = %@",self.dataArray);
        
        [self.myCollectionView reloadData];
        
    }failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
